package com.studyv3.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.studyv3.common.core.domain.PageBean;
import com.studyv3.common.core.domain.R;
import com.studyv3.test.domain.ApplyProduct;
import com.studyv3.test.domain.Batch;
import com.studyv3.test.mapper.ApplyProductMapper;
import com.studyv3.test.mapper.BatchMapper;
import com.studyv3.test.service.BatchService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
* @author 侯杰
* @description 针对表【lb_batch(lb_采购批次)】的数据库操作Service实现
* @createDate 2024-05-07 10:04:41
*/
@Service
@RequiredArgsConstructor
public class BatchServiceImpl implements BatchService{

    private final BatchMapper batchMapper;

    private final ApplyProductMapper applyProductMapper;



    @Override
    public R add(Batch add) {
        add.setCreateTime(LocalDateTime.now());
        add.setUpdateTime(LocalDateTime.now());
        if(batchMapper.selectOne(new LambdaQueryWrapper<Batch>().eq(Batch::getName,add.getName())) != null){
            return R.fail("批次已存在");
        }
        if (batchMapper.insert(add) > 0){
            return R.ok("添加成功");
        }
        return R.fail("添加失败");
    }

    @Override
    public R deleteUserByIds(Integer[] ids) {
        batchMapper.deleteBatchIds(Arrays.asList(ids));
        return R.fail("删除成功");
    }

    @Override
    public R queryAll() {
        return R.ok(batchMapper.selectList(new LambdaQueryWrapper<Batch>().orderByAsc(Batch::getName)));
    }

    @Override
    public PageBean<Batch> list(Integer pageNum, Integer pageSize, String a, String b, String c) {
        //条件分页
        //1.创建PageBean对象
        PageBean<Batch> pb = new PageBean<>();
        //2.开启分页查询(会自动拼接sql)
        PageHelper.startPage(pageNum,pageSize);
        //3.调用mapper
//        LambdaQueryWrapper<Batch> wrapper = new LambdaQueryWrapper<>();
//        if(!Objects.isNull(a)){
//            wrapper.like(Batch::getName,a);
//        }
//        if(!Objects.isNull(b)){
//            wrapper.ge(Batch::getEndTime,b);
//        }
//        if(!Objects.isNull(c)){
//            wrapper.le(Batch::getStartTime,c);
//        }
        List<Batch> as = batchMapper.selectAllList(a,b,c);
//        System.out.println(as);
        //4.结果转为PageHelper的page对象
        Page<Batch> p = (Page<Batch>) as;
        //Page提供了方法，可以获取PageHelper分页查询后得到的总记录条数和当前页数据
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public PageBean<Batch> listwarehous(Integer pageNum, Integer pageSize, String a, String b, String c) {
        //条件分页
        //1.创建PageBean对象
        PageBean<Batch> pb = new PageBean<>();
        //2.开启分页查询(会自动拼接sql)
        PageHelper.startPage(pageNum,pageSize);
        //3.调用mapper
//        LambdaQueryWrapper<Batch> wrapper = new LambdaQueryWrapper<>();
//        if(!Objects.isNull(a)){
//            wrapper.like(Batch::getName,a);
//        }
//        if(!Objects.isNull(b)){
//            wrapper.ge(Batch::getEndTime,b);
//        }
//        if(!Objects.isNull(c)){
//            wrapper.le(Batch::getStartTime,c);
//        }
        List<Batch> as = batchMapper.selectAllList(a,b,c);
        if(!as.isEmpty()){
            for(Batch item : as){
                item.setUnwarehoused(applyProductMapper.selectCount(Wrappers.<ApplyProduct>lambdaQuery()
                        .eq(ApplyProduct::getFlag,1)
                        .eq(ApplyProduct::getDeleted,0)
                        .eq(ApplyProduct::getWarehousFlag,0)
                        .eq(ApplyProduct::getBatchId,item.getId())).intValue());
                item.setWarehoused(applyProductMapper.selectCount(Wrappers.<ApplyProduct>lambdaQuery()
                        .eq(ApplyProduct::getFlag,1)
                        .eq(ApplyProduct::getDeleted,0)
                        .eq(ApplyProduct::getWarehousFlag,1)
                        .eq(ApplyProduct::getBatchId,item.getId())).intValue());
            }
        }

//        System.out.println(as);
        //4.结果转为PageHelper的page对象
        Page<Batch> p = (Page<Batch>) as;
        //Page提供了方法，可以获取PageHelper分页查询后得到的总记录条数和当前页数据
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }


    @Override
    public R update(Batch buyProducts) {
        buyProducts.setUpdateTime(LocalDateTime.now());
        if(batchMapper.updateById(buyProducts)>0){
            return R.ok("更新成功");
        }
        return R.fail("更新失败");
    }

    @Override
    public List<ApplyProduct> todoList(Integer id) {
        return applyProductMapper.selectList(new LambdaQueryWrapper<ApplyProduct>().eq(ApplyProduct::getBatchId,id)
                .eq(ApplyProduct::getFlag,2));
    }

    @Override
    public List<ApplyProduct> doneList(Integer id) {
        return applyProductMapper.selectList(new LambdaQueryWrapper<ApplyProduct>().eq(ApplyProduct::getBatchId,id)
                .ne(ApplyProduct::getFlag,2));
    }

    @Override
    public Batch findById(Integer id) {
        return batchMapper.selectId(id);
    }

    @Override
    public R findUseableBatch() {
        return R.ok(batchMapper.selectList(Wrappers.<Batch>lambdaQuery()
                .eq(Batch::getIo,1)
                .le(Batch::getStartTime, LocalDate.now())
                .ge(Batch::getEndTime,LocalDate.now())));

    }
}




